package com.woniu.wouniuYoga.comm.client.feign;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author: 付宽成
 * @description: feign的拦截器   用于微服务之间传递token参数
 * @date: 2020/8/24 9:41
 * @version: 1.0
 */
@Component
public class TokenInterceptor implements RequestInterceptor{
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //获取当前请求的参数对象
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        //获取请求头中的token
        String token = attributes.getRequest().getHeader("X-Token");
        System.out.println("X-Token = " + token);
        if (token != null&&!token.trim().equals("")){
            requestTemplate.header("X-Token",token);
        }
    }
}
